package com.voxel.sense.infomsg.module.user.personal.edit;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;

import com.example.com.common.app.BViewActivity;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.view.titlebar.TextTitleBar;

import butterknife.BindView;
import cyy.example.com.facotry.constants.Constants;
import cyy.example.com.facotry.constants.IntentConstants;
import cyy.example.com.facotry.share.SPEngine;

public class EditBaseInfoActivity extends BViewActivity<EditBaseInfoContract.Presenter> implements EditBaseInfoContract.View {
    @BindView(R.id.titlebar)
    TextTitleBar titlebar;
    @BindView(R.id.et_describe)
    EditText et_describe;

    private int mType;
    private String mOriginalText;


    /**
     * @param type 修改类型 0名字 1电话 2邮件
     */
    public static void show(Context context, int type) {
        Intent intent = new Intent(context, EditBaseInfoActivity.class);
        intent.putExtra(IntentConstants.STYPE, type);
        context.startActivity(intent);
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_edit_base_info;
    }

    @Override
    protected EditBaseInfoContract.Presenter initPresenter() {
        return new EditBaseInfoPresenter(this);
    }

    @Override
    protected boolean initArgs(Bundle bundle) {
        mType = bundle.getInt(IntentConstants.STYPE);
        if (mType == Constants.EDIT_Email || mType == Constants.EDIT_NAME || mType == Constants.EDIT_Phone) {
            return super.initArgs(bundle);
        } else {
            return false;
        }
    }

    @Override
    protected void initData() {
        super.initData();

    }

    @Override
    protected void initWidget() {
        super.initWidget();
        String title = "";
        String hintDes = "请输入";
        mOriginalText = "";
        SPEngine.UserInfo userInfo = SPEngine.getSPEngine().getUserInfo();
        if (mType == Constants.EDIT_NAME) {
            title = "修改名称";
            hintDes = "请输入姓名";
            String name = userInfo.getName();
            mOriginalText = name;
        } else if (mType == Constants.EDIT_Phone) {
            mOriginalText = userInfo.getPhone();
            title = "修改手机号";
            hintDes = "请输入手机号";
            et_describe.setInputType(InputType.TYPE_CLASS_PHONE);
            et_describe.setMaxEms(11);
        } else if (mType == Constants.EDIT_Email) {
            title = "修改邮箱";
            hintDes = "请输入邮箱";
            mOriginalText = userInfo.getEmail();
            et_describe.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        }
        titlebar.setTitleText(title);
        et_describe.setText(mOriginalText);
        et_describe.setHint(hintDes);
    }

    @Override
    protected void initEven() {
        super.initEven();
        titlebar.setRightTextAndLis("修改", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.doEditInfo(et_describe.getText().toString(), mType);
            }
        });
    }

    @Override
    public void onEditSuccess() {
        finish();
    }

    @Override
    public void onEditFailure() {

    }


}
